home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / coreaids.zip / HEX_ASC.ASM < prev    next >
Assembly Source File  |  1987-06-25  |  4KB  |  155 lines

  1. ;    DESC:    Convert Hex numbers to decimal in ASCII format       V1.03
  2. ;    IN:    *{HI_HEX} High order word of 4 byte hexadecimal number
  3. ;        *{LO_HEX} Low order word of 4 byte hexadecimal number
  4. ;    OUT:    *{ASC_1} five words from MSB to LSB with leading
  5. ;        *{ASC_2} zeros stripped
  6. ;        *{ASC_3}
  7. ;        *{ASC_4}
  8. ;        *{ASC_5}
  9. ;    SAMPLE:    Callm    HEX_ASC,<HI_HEX,LO_HEX>,
  10. ;                <ASC_1,ASC_2,ASC_3,ASC_4,ASC_5>
  11. ;    ##################################################################
  12.  
  13. HEX_ASCD Segment Para Public 'DATA'
  14.         DB    10 DUP(30H)
  15. ZEROS        DB    30H
  16.         DB    10 DUP(20H)
  17. BLANKS        DB    20H
  18. HIWRD        DW    0
  19. LOWRD        DW    0
  20. ZERO        DB    10 DUP(0)
  21. SET        DB    0
  22. CNT        DB    0
  23. POS        DW    0FFFFH
  24. TEN        DB    10
  25. TOTAL        DB    10 DUP(0)
  26. ENDTOT        DB    0
  27. MULTS        DB    1,9 DUP(0)
  28.         DB    6,1,8 DUP(0)
  29.         DB    6,5,2,7 DUP(0)
  30.         DB    6,9,0,4,6 DUP(0)
  31.         DB    6,3,5,5,6,5 DUP(0)
  32.         DB    6,7,5,8,4,0,1,3 DUP(0)
  33.         DB    6,1,2,7,7,7,6,1,2 DUP(0)
  34.         DB    6,5,4,5,3,4,8,6,2,0
  35. HEX_ASCD Ends
  36.  
  37.     Extrn    PUSHALL:Near
  38.     Extrn    POPALL:Near
  39.  
  40. HEX_ASCC    Segment
  41.     Assume CS:HEX_ASCC,DS:HEX_ASCD
  42.     Public    HEX_ASC
  43.                         ;notice.
  44.     DB    'HEX_ASC  - V1.00, Copyright 1987, CoreTechs   ',0DH,0AH
  45.  
  46. HEX_ASC    Proc    Near
  47.     Call    PUSHALL
  48.  
  49.     Mov    AX,HEX_ASCD            ;setup workarea.
  50.     Mov    DS,AX
  51.  
  52.     Pop    LOWRD                ;recover hex number.
  53.     Pop    HIWRD
  54.  
  55.     Mov    SET,0                ;clear counters.
  56.     Mov    CNT,0
  57.     Mov    POS,0FFFFH
  58.  
  59.     Push    DS                ;equate ES to workarea.
  60.     Pop    ES
  61.  
  62.     Mov    SI,OFFSET ZERO            ;clear total to zeros.
  63.     Mov    DI,OFFSET TOTAL
  64.     Mov    CX,10
  65.     Cld
  66.     Rep    MOVS    ES:BYTE PTR[DI],DS:[SI]
  67.  
  68. TOP:    Inc    POS                ;move to first nibble of 8.
  69.     Cmp    POS,8                ;if last nibble processed
  70.     Jz    FINI                ;then exit.
  71.  
  72.     Mov    AX,LOWRD            ;process low word first.
  73.     Cmp    POS,4                ;if >= 4th nibble then
  74.     Jb    LO
  75.     Mov    AX,HIWRD            ;process high word.
  76.  
  77. LO:    Mov    BX,POS                ;position is a measure of
  78.     Cmp    POS,4                ;4 when number is being
  79.     Jb    LO2                ;treated as high and low words
  80.     Sub    BX,4                ;so BX holds relative offset.
  81.  
  82. LO2:    Mov    CX,2                ;CX holds bit offset, so
  83.     Shl    BX,CL                ;position values of 1,2,3,4
  84.     Mov    CX,BX                ;correspond to bit values of
  85.                         ;4,8,12,16.
  86.  
  87.     Shr    AX,CL                ;all bits to right and left
  88.     And    AX,000FH            ;of subject nibble are
  89.                         ;cleared.
  90.  
  91.     Mov    CNT,AL                ;determines repetition count
  92.                         ;for looping.
  93.  
  94. DECWRD:    Cmp    CNT,0                ;is loop completed?
  95.     Jz    TOP                ;if so, work on next nibble.
  96.  
  97.     Dec    CNT                ;decrease loop counter.
  98.     Mov    AX,POS                ;compute offset into multi-
  99.     Mul    TEN                ;plier.
  100.  
  101.     Mov    BX,OFFSET MULTS            ;find total offset.
  102.     Add    BX,AX
  103.  
  104.     Mov    BP,OFFSET TOTAL            ;add number in total to
  105. NEXWRD:    Mov    AX,DS:WORD PTR[BP]        ;portion of multiplier.
  106.     Mov    CX,DS:WORD PTR[BX]
  107.     Add    AL,CL
  108.  
  109.     Aaa                    ;adjust numbers over 9 to
  110.     Mov    DS:WORD PTR[BP],AX        ;high part of word and resave.
  111.  
  112.     Cmp    BP,OFFSET ENDTOT-1        ;if total has been processed
  113.     Jz    DECWRD                ;handle next bit.
  114.  
  115.     Inc    BP                ;flip carry bit through
  116.     Inc    BX                ;entire total.
  117.     Jmp    NEXWRD
  118.  
  119. FINI:    Mov    BX,OFFSET TOTAL            ;convert bit string answer
  120.     Or    DS:WORD PTR[BX],3030H        ;to ASCII digits.
  121.     Or    DS:WORD PTR[BX+2],3030H
  122.     Or    DS:WORD PTR[BX+4],3030H
  123.     Or    DS:WORD PTR[BX+6],3030H
  124.     Or    DS:WORD PTR[BX+8],3030H
  125.  
  126.     Mov    SI,OFFSET ENDTOT-1        ;locate first nonzero digit
  127.     Mov    DI,OFFSET ZEROS            ;in result and convert
  128.     Mov    CX,10                ;leading zeros to blanks.
  129.     Std
  130.     Rep    CMPSB
  131.  
  132.     Mov    AX,OFFSET ZEROS            ;break out number of zeros.
  133.     Sub    AX,DI
  134.     Dec    AX
  135.  
  136.     Mov    SI,OFFSET BLANKS        ;replace with blanks.
  137.     Mov    CX,AX
  138.     Mov    DI,OFFSET ENDTOT-1
  139.     Std
  140.     Rep    MOVS    ES:BYTE PTR[DI],DS:[SI]
  141.  
  142.     Mov    BX,OFFSET TOTAL            ;return result in five words
  143.     Push    DS:WORD PTR[BX]            ;from MSW to LSW.
  144.     Push    DS:WORD PTR[BX+2]
  145.     Push    DS:WORD PTR[BX+4]
  146.     Push    DS:WORD PTR[BX+6]
  147.     Push    DS:WORD PTR[BX+8]
  148.  
  149.     Call    POPALL                ;restore registers.
  150.     Ret
  151.  
  152. HEX_ASC    Endp
  153. HEX_ASCC    Ends
  154.     End
  155.